package com.laysonx.data.police.core;

import com.laysonx.data.police.util.BeanDefinitionUtil;
import com.laysonx.data.police.util.CheckUtil;
import lombok.Data;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;

/**
 * JavaConfig 方式开启数据权限检查
 * @author: Laysonx
 * @date: 2019/9/27 16:18
 */
@Data
public class DataPoliceConfigurer implements BeanDefinitionRegistryPostProcessor,ResourceLoaderAware {

    private String[] scanHelperPackages ;

    private String pointcut ;

    private ResourceLoader resourceLoader;

    public DataPoliceConfigurer() {}

    public DataPoliceConfigurer(String[] scanHelperPackages, String pointcut) {
        this.scanHelperPackages = scanHelperPackages;
        this.pointcut = pointcut;
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {

        // 检查扫描路径、切点等初始化参数
        CheckUtil.checkInitParams(scanHelperPackages,pointcut);

        // 扫描@DataHelper
        BeanDefinitionUtil.scanDataHelper(registry,resourceLoader,scanHelperPackages);

        // 初始化所需beanDefinition
        BeanDefinitionUtil.initBean(registry,pointcut);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {}

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }
}


